const utils = require('../utils/util')
const URLS = require('../utils/url.js');
const request = async (url, param, method) => {
  if (!method) {
    method = URLS.REQUEST_METHOD_POST;
  }
  console.log(URLS.REQUEST_METHOD_POST);
  console.log(URLS.HttpServerHostAddress);
  console.log(url);
  if (url.indexOf('://') == -1) {
    url = URLS.HttpServerHostAddress + url;
  }
  console.log('param2', param)
  if(!param.token && param.token!= undefined) {
	  console.log('token', param.token)
	  
    const token = await utils.getToken()
    if(token) {
      param.token = token
    }
  }
  param.method = 'miniapp'; 
  console.log('param1', param)
  return new Promise((resolve, reject) => {
    wx.request({
      url: url, //仅为示例，并非真实的接口地址
      method: method,
      dataType: 'json',
      data: param,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        resolve(res);
      },
      fail(error) {
        console.log('error-----', url, '===', error);
        reject(error);
      }
    });
  });
}


module.exports = {
  request: request
}